library(stringr)
library(plyr)
library(foreign)
library(plm)
library(apsrtable)

load('traffic.survey.RData')

espn.media.full <- lm(espn.trust.standard ~ visit.alignment +  I(espn.visits.per.day > 0) + full.pid.scale + income + race + gender + age + political.interest + ideology + education, data=traffic.survey)
espn.media.dem <- lm(espn.trust.standard ~ visit.alignment +  I(espn.visits.per.day > 0) + full.pid.scale + income + race + gender + age + political.interest + ideology + education, data=traffic.survey, subset=which(full.pid.scale %in% c('Strong Democrat','Not very strong Democrat','Lean Democrat')))
espn.media.ind <- lm(espn.trust.standard ~ visit.alignment +  I(espn.visits.per.day > 0) + income + race + gender + age + political.interest + ideology + education, data=traffic.survey, subset=which(full.pid.scale=='Independent'))
espn.media.rep <- lm(espn.trust.standard ~ visit.alignment +  I(espn.visits.per.day > 0) + full.pid.scale + income + race + gender + age + political.interest + ideology + education, data=traffic.survey, subset=which(full.pid.scale %in% c('Strong Republican','Not very strong Republican','Lean Republican')))

#Table 2
apsrtable(espn.media.full,espn.media.dem,espn.media.ind,espn.media.rep,caption="Trustworthiness of ESPN by Online Media Exposure",model.names=c('All','Democrats','Independents','Republicans'),omitcoef=expression(grep(pattern="(Intercept)|alignment|espn",coefnames,invert=TRUE)),coef.names=c("(Intercept)","Partisanship of Political News Use","ESPN Visitor"),notes=list('Models condition on partisanship and demographic variables',se.note,stars.note))

espn.media.alt1 <- lm(espn.trust.standard ~ visit.alignment +  espn.visits.per.day.trimmed + full.pid.scale + income + race + gender + age + political.interest + ideology + education, data=traffic.survey)
espn.media.alt2 <- lm(espn.trust.standard ~ visit.alignment + sports.diff + full.pid.scale + income + race + gender + age + political.interest + ideology + education, data=traffic.survey)

fox.media.alt1 <- lm(espn.trust.standard ~ I(fox.visits.per.day.trimmed>0) +  I(espn.visits.per.day.trimmed>0) + full.pid.scale + income + race + gender + age + political.interest + ideology + education, data=traffic.survey,subset=which(!is.na(visit.alignment)))
fox.media.alt2 <- lm(espn.trust.standard ~ fox.visits.per.day.trimmed +  I(espn.visits.per.day.trimmed>0) + full.pid.scale + income + race + gender + age + political.interest + ideology + education, data=traffic.survey,subset=which(!is.na(visit.alignment)))

#Appendix Table E2
apsrtable(fox.media.alt1,fox.media.alt2,espn.media.alt1,espn.media.alt2,caption="Trustworthiness of ESPN by Online Media Exposure",model.names=c('Alternative Fox 1',"Alternative Fox 2",'Alternative ESPN 1','Alternative ESPN 2'),omitcoef=expression(grep(pattern="(Intercept)|alignment|espn|fox|sports",coefnames,invert=TRUE)),coef.names=c("(Intercept)","Fox Visitor","ESPN Visitor","Fox Daily Visits","Partisan Media Use","ESPN Daily Visits","ESPN-Other Sports Daily Visits"),notes=list('Models condition on partisanship and demographic variables',se.note,stars.note))

###
#Interaction Model
###

espn.media.interaction <- lm(espn.trust.standard ~ visit.alignment*I(full.pid.scale %in% c('Independent')) + visit.alignment*I(full.pid.scale %in% c('Strong Republican','Not very strong Republican','Lean Republican')) +  I(espn.visits.per.day > 0) + income + race + gender + age + political.interest + ideology + education, data=traffic.survey)

#Table E3
apsrtable(espn.media.interaction,caption="Trustworthiness of ESPN by Online Media Exposure",model.names=c('Interaction'),omitcoef=expression(grep(pattern="(Intercept)|alignment|espn|fox|sports|pid.scale",coefnames,invert=TRUE)),notes=list('Models condition on partisanship and demographic variables','Partisan Reference Category is Democrats',se.note,stars.note),coef.names=c("(Intercept)","Right-Leaning Media Exposure","Independent","Republican","ESPN Visitor","Right-Leaning Media Exposure*Independent","Right-Leaning Media Exposure*Republican"))
          
###
#Partisanship and News Usage
###

#Binary Indicator
pid.visits.binary <- lm(I(espn.visits.per.day.trimmed>0) ~ pid_3pt, data=traffic.survey)
pid.visits.binary$se <- sqrt(diag(vcovHC(pid.visits.binary,type="HC1")))

#Continuous Indicator
pid.visits.continuous <- lm(espn.visits.per.day.trimmed ~ pid_3pt, data=traffic.survey)

#Relative Share
pid.visits.difference <- lm(sports.diff ~ pid_3pt, data=traffic.survey)

#Table 5
apsrtable(pid.visits.binary,pid.visits.continuous,pid.visits.difference,caption="Visits to ESPN's Website by Party",model.names=c('Binary','Continuous','Difference'),omitcoef=expression(grep(pattern="(Intercept)|pid|sports|espn",coefnames,invert=TRUE)),coef.names=c("(Intercept)","Independent","Republican"))


###
#W1 Party and Media Usage on Views of ESPN
###

#Partisan Media and Use (W1 ESPN Use and Party ID Measure)
espn.media.full.w1 <- lm(espn.trust.standard ~ visit.alignment +  I(espn.visits.per.day.w1> 0) + full.pid.scale.w1 + income + race + gender + age + political.interest + ideology + education, data=traffic.survey)
espn.media.dem.w1 <- lm(espn.trust.standard ~ visit.alignment +  I(espn.visits.per.day.w1 > 0) + full.pid.scale.w1 + income + race + gender + age + political.interest + ideology + education, data=traffic.survey, subset=which(full.pid.scale.w1 %in% c('Strong Democrat','Not very strong Democrat','Lean Democrat')))
espn.media.ind.w1 <- lm(espn.trust.standard ~ visit.alignment +   I(espn.visits.per.day.w1 > 0) + income + race + gender + age + political.interest + ideology + education, data=traffic.survey, subset=which(full.pid.scale.w1=='Independent'))
espn.media.rep.w1 <- lm(espn.trust.standard ~ visit.alignment +  I(espn.visits.per.day.w1 > 0) + full.pid.scale.w1 + income + race + gender + age + political.interest + ideology + education, data=traffic.survey, subset=which(full.pid.scale.w1 %in% c('Strong Republican','Not very strong Republican','Lean Republican')))

#Table E4
apsrtable(espn.media.full.w1,espn.media.dem.w1,espn.media.ind.w1,espn.media.rep.w1,caption="Trustworthiness of ESPN by Online Media Exposure (2016 ESPN Visitor)",model.names=c('All','Democrats','Independents','Republicans'),omitcoef=expression(grep(pattern="(Intercept)|alignment|espn",coefnames,invert=TRUE)),coef.names=c("(Intercept)","Partisan Media Exposure","ESPN Visitor"),notes=list('Models condition on partisanship and demographic variables',se.note,stars.note))

###
#Temporal Variation in ESPN Visits
###

load('sports.visits.bymonth.RData')

unique.months <- unique(sports.visits.bymonth$month.year)
unique.months <- unique.months[order(unique.months)]

rep.diff.continuous <- NA
rep.diff.continuous.se <- NA

rep.diff.binary <- NA
rep.diff.binary.se <- NA

rep.diff.difference <- NA
rep.diff.difference.se <- NA

for(k in 1:length(unique.months)){
	
	continuous.model <- lm(espn.visits.per.day.trimmed ~ pid.panel,data=sports.visits.bymonth,subset=which(month.year==unique.months[k]))
	rep.diff.continuous[k] <- continuous.model$coefficients[3]
	rep.diff.continuous.se[k] <- summary(continuous.model)$coefficients[3,2]

	binary.model <-  lm( I(espn.visits.per.day.trimmed > 0) ~ pid.panel,data=sports.visits.bymonth,subset=which(month.year==unique.months[k]))
	binary.model$se <- sqrt(diag(vcovHC(binary.model,type="HC1")))
	rep.diff.binary[k] <- binary.model$coefficients[3]
	rep.diff.binary.se[k] <- binary.model$se[3]
	
	difference.model <-  lm( I(espn.visits.per.day.trimmed - other.sports.visits.per.day.trimmed) ~ pid.panel,data=sports.visits.bymonth,subset=which(month.year==unique.months[k]))
	rep.diff.difference[k] <- difference.model$coefficients[3]
	rep.diff.difference.se[k] <- summary(difference.model)$coefficients[3,2]
}

out.frame.binary <- cbind.data.frame(unique.months,rep.diff.binary,rep.diff.binary.se)
out.frame.binary$upper <- out.frame.binary$rep.diff.binary + 2*out.frame.binary$rep.diff.binary.se
out.frame.binary$lower <- out.frame.binary$rep.diff.binary - 2*out.frame.binary$rep.diff.binary.se

out.frame.continuous <- cbind.data.frame(unique.months,rep.diff.continuous,rep.diff.continuous.se)
out.frame.continuous$upper <- out.frame.continuous$rep.diff.continuous + 2*out.frame.continuous$rep.diff.continuous.se
out.frame.continuous$lower <- out.frame.continuous$rep.diff.continuous - 2*out.frame.continuous$rep.diff.continuous.se

out.frame.difference <- cbind.data.frame(unique.months,rep.diff.difference,rep.diff.difference.se)
out.frame.difference$upper <- out.frame.difference$rep.diff.difference + 2*out.frame.difference$rep.diff.difference.se
out.frame.difference$lower <- out.frame.difference$rep.diff.difference - 2*out.frame.difference$rep.diff.difference.se

#Figure 3

pdf(file='figure3.pdf',height=3.5,width=6.5)
par(mar=c(4,5,.25,.25))
plot(x=1:length(unique.months),y=out.frame.continuous$rep.diff.continuous,pch=16,ylim=c(-0.5,0.55),las=1,main="",xlab="",xaxt='n',ylab='Average Daily Visit Difference
(Republicans - Democrats)',type='n')
abline(v=5.5)
abline(v=17.5)
abline(h=0,lty=2)
#lines(x=smoothed.relationship$x,y=smoothed.relationship$y,lwd=4,col='gray')
points(x=1:length(unique.months),y=out.frame.continuous$rep.diff.continuous,pch=16,cex=1.75)
segments(x0=1:length(unique.months),y0=out.frame.continuous$lower,y1=out.frame.continuous$upper,lwd=3)
axis(side=1,at=c(1:20),labels=c('Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar'),las=2)
axis(side=1,at=c(3,11.5,19),labels=c(2016,2017,2018),line=2,lwd=0)
dev.off()

#Figure F2

pdf(file='figuref2.pdf',height=3.5,width=6.5)
par(mar=c(4,5,.25,.25))
plot(x=1:length(unique.months),y=out.frame.binary$rep.diff.continuous,pch=16,ylim=c(-0.15,0.2),las=1,main="",xlab="",xaxt='n',ylab='Average Probability of Visit
(Republicans - Democrats)',type='n')
abline(v=5.5)
abline(v=17.5)
abline(h=0,lty=2)
#lines(x=smoothed.relationship$x,y=smoothed.relationship$y,lwd=4,col='gray')
points(x=1:length(unique.months),y=out.frame.binary$rep.diff.binary,pch=16,cex=1.75)
segments(x0=1:length(unique.months),y0=out.frame.binary$lower,y1=out.frame.binary$upper,lwd=3)
axis(side=1,at=c(1:20),labels=c('Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar'),las=2)
axis(side=1,at=c(3,11.5,19),labels=c(2016,2017,2018),line=2,lwd=0)
dev.off()

#Figure F3

pdf(file='figuref3.pdf',height=3.5,width=6.5)
par(mar=c(4,5,.25,.25))
plot(x=1:length(unique.months),y=out.frame.difference$rep.diff.difference,pch=16,ylim=c(-0.5,0.5),las=1,main="",xlab="",xaxt='n',ylab='Average Daily Visit Diff ESPN/Others
(Republicans - Democrats)',type='n')
abline(v=5.5)
abline(v=17.5)
abline(h=0,lty=2)
#lines(x=smoothed.relationship$x,y=smoothed.relationship$y,lwd=4,col='gray')
points(x=1:length(unique.months),y=out.frame.difference$rep.diff.difference,pch=16,cex=1.75)
segments(x0=1:length(unique.months),y0=out.frame.difference$lower,y1=out.frame.difference$upper,lwd=3)
axis(side=1,at=c(1:20),labels=c('Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar'),las=2)
axis(side=1,at=c(3,11.5,19),labels=c(2016,2017,2018),line=2,lwd=0)
dev.off()
